package com.android.thought.badboys;

import java.util.ArrayList;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;

import com.android.thought.badboys.Info.PhoneInfo;

public class Services extends Service implements SensorEventListener {

	@Override
	public void onCreate() {

		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		accelerormeterSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	

		super.onCreate();
	}
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		onAlarmStart();
		


		if (accelerormeterSensor != null)
			sensorManager.registerListener(this, accelerormeterSensor, SensorManager.SENSOR_DELAY_GAME);
		
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {

		if (sensorManager != null)
			sensorManager.unregisterListener(this);
		super.onDestroy();
	}

	
	void onAlarmStart() {
		SharedPreferenceUtil.getInstance(getApplicationContext());
		boolean isCheck = SharedPreferenceUtil.getValue(SharedPreferenceUtil.PREF_INTRO_USER_AUTO_MESSAGE, false);
		
		final Application app = getApplication();
		
		if ( app != null && app instanceof BadBoysApplication ) {
			Model model = ((BadBoysApplication)app).getModel();
		
		
		if ( isCheck ) {
			try {
				ArrayList<PhoneInfo> infos = model.getAdministrationPhoneInfo().getList();
				
				if ( infos != null && infos.size() > 0 && isCheck ) {
					Model.getAlarmServicesInstance(this).onRegister(infos);
				} else {
					Model.getAlarmServicesInstance(this).onDelete();
				}
			
			} catch (Exception e) { }
		}
		}
	}
	
	
		private long lastTime;
		private float speed;
		private float lastX;
		private float lastY;
		private float lastZ;
		private float x, y, z;
		private static final int SHAKE_THRESHOLD = 800;
		private static final int DATA_X = 0;
		private static final int DATA_Y = 1;
		private static final int DATA_Z = 2;
		private SensorManager sensorManager;
		private Sensor accelerormeterSensor;
		
		

		


		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				long currentTime = System.currentTimeMillis();
				long gabOfTime = (currentTime - lastTime);
				if (gabOfTime > 100) {
					lastTime = currentTime;
					x = event.values[DATA_X];
					y = event.values[DATA_Y];
					z = event.values[DATA_Z];
					speed = Math.abs(x + y + z - lastX - lastY - lastZ) / gabOfTime * 10000;
					if (speed > SHAKE_THRESHOLD) {
						// 이벤트발생!! } 
						lastX = event.values[DATA_X]; 
						lastY = event.values[DATA_Y];
						lastZ = event.values[DATA_Z];
						Log.e("CH_DEBUG", "##################### 시작 이벤트 : " );
						
					}
				}
			}
		}
	

}
